/**
File: csuToolWriteSubspaceAsMatrices.c
Authors: Marcio Luis Teixeira                           
Date: May 24, 2002                                                       
*/

/*
Copyright (c) 2003 Colorado State University

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#define OPENING  "Saves subspace files as matrices"

/*
 Purpose: This program writes subspace training file (from either PCA or the
Bayesian classifier) as text files suitable for loading into MatLab or Octave.
*/

/*  CSU INCLUDES */
#include <csuCommon.h>

/*
 The command line arguments are managed by a single structure.
*/

typedef struct
{
  char* trainingFile;
  MatrixSaveMode mode;
}
Arguments;

/* ----------------------------------------------------------------------------------- */
/* Remind the user the usage of running this program.
*/
void usage(const char* prog)
{
  printf("Usage: %s [OPTIONS] trainingFile\n", prog);
  printf("  Parameters:\n");
  printf("     trainingFile ....    Name of file containing subspace generated by training.\n");
  printf("    -debuglevel <int>:    Level of debug information to display. DEFAULT = 0\n");
  printf("    -quiet:               Turn off all messages. DEFAULT = messages on\n");
  printf("    -matlab:              Save matrices in matlab format. DEFAULT = octave\n");

  exit(1);
}

void process_command(int argc, char** argv, Arguments* args)
{
    int i;
    int param_num = 0;

    /******* Set up default values *******/

    args->mode = octaveFormat;

    quiet = 0;
    debuglevel = 0;

    for (i = 1; i < argc; i++) {

      /* Catch common help requests */
      if      (readOption       (argc, argv, &i, "-help" )) { usage(argv[0]); }
      else if (readOption       (argc, argv, &i, "--help")) { usage(argv[0]); }

      /* other flags */
      else if (readOption       (argc, argv, &i, "-quiet")) { quiet = 1; }
      else if (readOptionInt    (argc, argv, &i, "-debuglevel", &debuglevel));
      else if (readOption       (argc, argv, &i, "-matlab")) { args->mode = matlabFormat; }

      /* check if the current argument is an unparsed option */
      else if (checkBadOption(argc,argv,&i));
      
      else if (param_num == 0) {
	args->trainingFile = strdup(argv[i]);
	param_num++;
      }
    }

    if (param_num < 1)
        usage (argv[0]);

  /* Print out the program parameters for appropriate debug level */

  DEBUG_INT (1, "Debuging enabled", debuglevel);

  if (debuglevel > 0)
    {
      printf ("***************** Program Parameters *********************\n");
      printf ("trainingFile: %s\n", args->trainingFile);
    }
}

/* ===========================================================================
 MAIN
 
 The arguments are processed and then the subspace and related information is
 read from the training file written by csuSubspaceTrain.  The subspace basis
 is read into a matrix and written out as a file.
*/

int main(int argc, char *argv[])
{
  Arguments args;
  Subspace subspace;
  char fname [255], label [255];

  MESSAGE (OPENING);
  MESSAGE (VERSION);
  
  process_command(argc, argv, &args);

  MESSAGE1ARG ("Reading subspace %s", args.trainingFile);
  readSubspace (&subspace, args.trainingFile, quiet);

  /* Save out the mean */

  strcpy (label, args.trainingFile);
  strcat (label, "_mean");
  strcpy (fname, label);
  strcat (fname, ".txt");
  MESSAGE1ARG ("Saving mean to %s", fname);
  saveMatrixAscii (fname, label, subspace.mean, args.mode);

  /* Save out the values */

  strcpy (label, args.trainingFile);
  strcat (label, "_values");
  strcpy (fname, label);
  strcat (fname, ".txt");
  MESSAGE1ARG ("Saving values to %s", fname);
  saveMatrixAscii (fname, label, subspace.values, args.mode);

  /* Save out the basis */

  strcpy (label, args.trainingFile);
  strcat (label, "_basis");
  strcpy (fname, label);
  strcat (fname, ".txt");
  MESSAGE1ARG ("Saving basis to %s", fname);
  saveMatrixAscii (fname, label, subspace.basis, args.mode);

  return 0;
}
